package com.amat0urheu.controller.adminController;

import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.annotation.OperateLog;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.CloudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class CloudController {
    @Autowired
    CloudService cloudService;

    //    @PreAuthorize("hasAuthority('ROLE_user')")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/cloud/getAllOpenstack/{pageSize}/{pageNum}")
    ReturnRes getAllOpenstack(@PathVariable int pageSize, @PathVariable int pageNum) {

        return cloudService.getAllOpenstack(pageSize, pageNum);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="1",action="查看了模板")
    @GetMapping("/admin/cloud/getAllOpentackIPs/{pageSize}/{pageNum}")
    ReturnRes getAllOpentackIP(@PathVariable int pageSize, @PathVariable int pageNum) {

        return cloudService.getAllOpenstackIPS(pageSize, pageNum);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="更新了openstack")
    //    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/cloud/updateOpenstack")
    public ReturnRes updateOpenstack(@RequestParam(name = "data") String OpenstackJson) {
        OpenStack openStack = JSONObject.parseObject(OpenstackJson, OpenStack.class);

        return cloudService.updateOpenstack(openStack);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="存储Openstack")
    @PostMapping("/admin/cloud/saveOpenstack")
    public ReturnRes RequestParam(@RequestParam(name = "data") String OpenstackJson) {
        OpenStack openStack = JSONObject.parseObject(OpenstackJson, OpenStack.class);
        return cloudService.saveOpenstack(openStack);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/cloud/removeOpenstack")
    public ReturnRes removeOpenstack(@RequestParam(name = "data") String OpenstackJson) {
        OpenStack openStack = JSONObject.parseObject(OpenstackJson, OpenStack.class);

        return cloudService.removeOpenstackById(openStack.getId());
    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/cloud/getAllHaveOpenstackIPs")
    ReturnRes getAllOpentackIP() {
        return new ReturnRes(true, cloudService.getAllOpenstackIP());
    }


}